<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <div class="mat">
        <h3 class="title">辅助开发助手</h3>
      </div>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="账号" prefix-icon="el-icon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import useUserStore from "@/store/modules/user";
import { ElMessage } from "element-plus";
import router from "@/router";

const userStore = useUserStore();
const loginRef = ref(null);

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};
const loading = ref(false);
const handleLogin = async () => {
  loading.value = true;
  try {
    await loginRef.value.validate();          // 先校验表单
    await userStore.login(loginForm.value);   // 再调接口
    ElMessage.success('登录成功');
    // 跳首页
    await router.push('/');
  } catch (e) {
    console.error(e);   // 任何异常都会进这里
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="less" scoped>
.mat {
  margin-bottom: 15px;
}

.title {
  color: #fff;
  text-align: center;
  font-weight: 700;
}

.login {
  display: flex;
  justify-content: flex-start; // 原来是 center
  align-items: center;
  padding-left: 68%; // 👈 控制左边留白
  width: 100%;
  height: 100%;
  background-image: url("@/assets/img/login/login-background.png");
  background-size: cover;


  .login-form {
    width: 400px;
    padding: 40px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background: #fff;


    .title {
      text-align: center;
      margin-bottom: 30px;
      font-size: 24px;
      color: #333;
      margin: 0;
      padding: 0;
    }
  }
}
</style>